package com.cw.config;

import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * 院前准备数据源
 * @author: ly
 * @date: 2021/05/05 10:51
 **/
@Configuration
@MapperScan(basePackages = "com.cw.mapper3",sqlSessionFactoryRef = "source3SqlSessionFactory")
public class DataSource3Config {

    @Bean(name = "test3DataSource")
    @ConfigurationProperties(prefix = "spring.datasource3")
    public DataSource getDateSource2() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "source3SqlSessionFactory")
    public SqlSessionFactory test2SqlSessionFactory(@Qualifier("test3DataSource") DataSource datasource)
            throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper3/*.xml"));
        return bean.getObject();
    }

    @Bean("test3SqlSessionTemplate")
    public SqlSessionTemplate test2sqlsessiontemplate(@Qualifier("source3SqlSessionFactory") SqlSessionFactory sessionfactory) {
        return new SqlSessionTemplate(sessionfactory);
    }

}
